package com.woniuxy.huayoung.service.impl;

import com.woniuxy.huayoung.controller.TravelGoodsController;
import com.woniuxy.huayoung.entity.Order;
import com.woniuxy.huayoung.entity.TravelGoods;
import com.woniuxy.huayoung.mapper.TravelGoodsMapper;
import com.woniuxy.huayoung.mapper.TravelGoodsOrderMapper;
import com.woniuxy.huayoung.service.TravelGoodsOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


@Service
public class TravelGoodsOrderServiceImpl implements TravelGoodsOrderService {


    //日志
    private final Logger LOOGER = LoggerFactory.getLogger(TravelGoodsOrderServiceImpl.class);

    @Resource
    private TravelGoodsMapper travelGoodsMapper;

    @Resource
    private TravelGoodsOrderMapper travelGoodsOrderMapper;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    private String orderno = null;

    //生成订单信息添加到数据库
    @Override
    public String addOrder(Order order) {


        //根据传回的商品goodsid获取到旅游商品信息
        TravelGoods travelGoods = travelGoodsMapper.findGoodsById(order.getGoodsid());

        //将相应商品详情信息赋值给order类中的travelgoods字段
        order.setTravelGoods(travelGoods);

        //通过UUID生成唯一订单号给orderno赋值
        String uuid = UUID.randomUUID().toString();
        orderno = uuid.replace("-","");
        order.setOrderno(orderno);

        //通过token解析获取手机号，再通过手机号获取到uid
            //假设获取到了uid为1
        order.setUid(1);


        //给type字段赋值  商品类型，1为旅游2为酒店，3为机票
        order.setType(1);

        //goodsid和num已经通过前端传值

        //生成当前请求的时间给order_time赋值
        LocalDateTime localDateTime = LocalDateTime.now();
            //将LocalDateTime转变为Date的方法
            Date date = Date.from( localDateTime.atZone( ZoneId.systemDefault()).toInstant());
            order.setOrderTime(date);


        //当前的订单状态为0  0未处理，1已接受，2未消费，已消费、4无效订单
        order.setStatus(0);

        //设置金额小计money  商品单价price和num相乘
        BigDecimal totalmoney = travelGoods.getPrice().multiply(new BigDecimal(order.getNum()));
        order.setMoney(totalmoney);

        //设置支付状态为0  0表示未支付 1表示支付了
        order.setPayType(0);

        //将订单信息添加到数据库中
        travelGoodsOrderMapper.addOrder(order);

        return orderno;
    }


    //根据订单号查询订单信息
    @Override
    public Order findByOrderNo(String orderno) {
        return travelGoodsOrderMapper.findByOrderNo(orderno);
    }

    //第一次查询订单返回订单信息到列表
    @Override
    public Order firstFindOrder(String orderno) {
        //将订单号放入redis中并设置15分钟的过期时间
        redisTemplate.opsForValue().set(orderno,orderno);
        redisTemplate.expire(orderno,15, TimeUnit.MINUTES);

        LOOGER.debug("将订单号放入redis缓存中成功");

        Order order = this.findByOrderNo(orderno);
        return order;
    }

    //根据订单编号查询订单并对该订单能否正常支付进行审核
    @Override
    public Integer payCheck(String orderno) {
    //检查商品数量是否满足商品需求
        //根据订单号查询获得该订单对应的旅游商品信息
        Order order = travelGoodsOrderMapper.findByOrderNo(orderno);
        //获取到该订单号商品的库存量
        Integer num = order.getTravelGoods().getNum();
        //该订单的购买需求量
        Integer orderRequiredNum = order.getNum();
        //获取相应旅游商品的状态
        Integer status = order.getTravelGoods().getStatus();
        if(status.equals(5)){
            //返回旅游商品状态为5 表示商品已下架 返回3
            return 3;
        } else if (num.intValue() < orderRequiredNum.intValue()){
            //库存量不足 返回2
            return 2;
        } else {
            //可以支付  返回1
            return 1;
        }
    }
}

